package com.kbbywyclient.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ImageUpdate;
import com.kbbywyclient.data.entity.WorkCardImages;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.InfoCertifyLoanModule;
import com.kbbywyclient.presenter.InfoCertifyLoanPersenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.InfoCertifyLoanContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.GalleryFinalUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.BottomPopupOption;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Description: 贷款 资料认证
 * Created by FHW
 * on 2017/5/3
 */

public class InfoCertifyLoanActivity extends BaseActivity implements InfoCertifyLoanContract.View {

    @BindView(R.id.v_toolbar)
    CustomToolBar v_toolbar;
    @BindView(R.id.iv_top_bg)
    ImageView iv_top_bg;
    @BindView(R.id.tv_certify_one)
    TextView tv_certify_one;
    @BindView(R.id.tv_certify_two)
    TextView tv_certify_two;

    @BindView(R.id.ll_work_permit)
    LinearLayout ll_work_permit;
    @BindView(R.id.ll_id_card)
    LinearLayout ll_id_card;
    @BindView(R.id.ll_business)
    LinearLayout ll_business;

    @BindView(R.id.ll_person_one)
    LinearLayout ll_person_one;
    @BindView(R.id.ll_company_two)
    LinearLayout ll_company_two;

    @BindView(R.id.iv_work_permit)
    ImageView iv_work_permit;
    @BindView(R.id.iv_id_card)
    ImageView iv_id_card;
    @BindView(R.id.iv_business)
    ImageView iv_business;

    @Inject
    InfoCertifyLoanPersenter infoCertifyLoanPersenter;

    private String userType = "";//身份类型，1为个人(顾问)，2为公司(机构)
    private int stepNum = 1;
    private int imgType = 0;//0-工作信息 1-身份证 2-营业执照

    private BottomPopupOption bottomPopupOption;//pop弹窗

    private String salesType = "loans";

    private String workCardImages1 = "";
    private String workCardImages2 = "";
    private String workCardImages3 = "";

    private int mType = 0;//0-贷款业务 1-信用卡业务

    private CustomDialog dialog;

    private boolean isModify = false;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_info_certify_loan;
    }

    @OnClick({R.id.tv_next, R.id.tv_submit, R.id.iv_work_permit, R.id.iv_id_card, R.id.iv_business})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                if (stepNum == 1) {
                    if (verify()) {
                        stepNum = 2;
                        setStepView();
                    }
                } else {
                    if (StringUtil.isNotEmpty(workCardImages2)) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("mType", mType);
                        ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class);
//                        EventBus.getDefault().post(new Event.SettingUpdate("5"));
                        finish();
                    } else {
                        ToastUtil.showShort("请上传身份证");
                    }
                }
                break;
            case R.id.tv_submit:
                if (StringUtil.isNotEmpty(workCardImages3)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("mType", mType);
                    ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class);
//                    EventBus.getDefault().post(new Event.SettingUpdate("5"));
                    finish();
                } else {
                    ToastUtil.showShort("请上传营业执照");
                }
                break;
            case R.id.iv_work_permit:
                imgType = 0;
                bottomPopupOption.showPopupWindow();
                break;
            case R.id.iv_id_card:
                imgType = 1;
                bottomPopupOption.showPopupWindow();
                break;
            case R.id.iv_business:
                imgType = 2;
                bottomPopupOption.showPopupWindow();
                break;
        }
    }

    private boolean verify() {
        if (StringUtil.isEmpty(workCardImages1)) {
            ToastUtil.showShort("请上传工作证");
            return false;
        }
        if (userType.equals("2")) {
            if (StringUtil.isEmpty(workCardImages2)) {
                ToastUtil.showShort("请上传身份证");
                return false;
            }
        }
        return true;
    }

    /**
     * 第二步的 界面
     */
    private void setStepView() {
        iv_top_bg.setBackgroundResource(R.mipmap.img_certify_two);
        tv_certify_two.setTextColor(getResources().getColor(R.color.color_02D1EA));
        switch (userType) {
            case "1":
                ll_work_permit.setVisibility(View.GONE);
                ll_id_card.setVisibility(View.VISIBLE);
                ll_business.setVisibility(View.GONE);

                ll_person_one.setVisibility(View.VISIBLE);
                ll_company_two.setVisibility(View.GONE);
                break;
            case "2":
                ll_work_permit.setVisibility(View.GONE);
                ll_id_card.setVisibility(View.GONE);
                ll_business.setVisibility(View.VISIBLE);

                ll_person_one.setVisibility(View.GONE);
                ll_company_two.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("贷款-资质认证");
    }

    @Override
    protected void initView() {

        mType = getIntent().getIntExtra("mType", 0);
        isModify = getIntent().getBooleanExtra("isModify", false);

        v_toolbar.setLeftViewOnClickLinster(v -> finish());

        bottomPopupOption = new BottomPopupOption(this);
        bottomPopupOption.setItemText("拍 照", "手机相册");
        bottomPopupOption.setItemClickListener(position -> {
            switch (position) {
                case 0://拍 照
                    GalleryFinalUtil.openCamera(mOnHanlderResultCallback);
                    break;
                case 1://手机相册
                    GalleryFinalUtil.openGallerySingle(mOnHanlderResultCallback);
                    break;
            }
            bottomPopupOption.dismiss();
        });
    }

    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            setImaView(resultList);
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };

    /**
     * 设置图片
     *
     * @param resultList
     */
    private void setImaView(List<PhotoInfo> resultList) {
        File path = new File(resultList.get(0).getPhotoPath());
        switch (imgType) {
            case 0:
                infoCertifyLoanPersenter.getImagesUpload(PreUtils.getUser(this).getSalesID() + "", salesType
                        , "WorkCardImages1", path);
                break;
            case 1:
                infoCertifyLoanPersenter.getImagesUpload(PreUtils.getUser(this).getSalesID() + "", salesType
                        , "WorkCardImages2", path);
                break;
            case 2:
                infoCertifyLoanPersenter.getImagesUpload(PreUtils.getUser(this).getSalesID() + "", salesType
                        , "WorkCardImages3", path);
                break;
        }
    }

    @Override
    public void initData() {
        infoCertifyLoanPersenter.getWorkCardImages(PreUtils.getUser(this).getSalesID() + "");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getInfoCertifyLoanComponent(new InfoCertifyLoanModule(this)).inject(this);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadSucWorkCard(WorkCardImages workCardImages) {
        userType = workCardImages.getUserType();
        workCardImages1 = workCardImages.getWorkCardImages1();
        workCardImages2 = workCardImages.getWorkCardImages2();
        workCardImages3 = workCardImages.getWorkCardImages3();

        HashMap<String, Object> map = new HashMap<>();
        map.put("mType", mType);

        switch (userType) {
            case "":
                showInfoDialog();
                break;
            case "1"://个人
                ll_work_permit.setVisibility(View.VISIBLE);
                if (StringUtil.isNotEmpty(workCardImages1) && StringUtil.isNotEmpty(workCardImages2)) {
                    if (!isModify) {
                        ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
                        finish();
                    }
                }
                break;
            case "2"://公司
                tv_certify_one.setText("工作/身份证");
                tv_certify_two.setText("公司信息");
                ll_work_permit.setVisibility(View.VISIBLE);
                ll_id_card.setVisibility(View.VISIBLE);

                if (StringUtil.isNotEmpty(workCardImages1) && StringUtil.isNotEmpty(workCardImages2) && StringUtil.isNotEmpty(workCardImages3)) {
                    if (!isModify) {
                        ActivitySkipUtil.skipAnotherActivity(this, InfoCertifySucActivity.class, map);
                        finish();
                    }
                }
                break;
        }
        if (StringUtil.isNotEmpty(workCardImages1)) {
            Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + workCardImages1).resize(235, 140).centerCrop().into(iv_work_permit);
        }
        if (StringUtil.isNotEmpty(workCardImages2)) {
            Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + workCardImages2).resize(235, 140).centerCrop().into(iv_id_card);
        }
        if (StringUtil.isNotEmpty(workCardImages3)) {
            Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + workCardImages3).resize(235, 140).centerCrop().into(iv_business);
        }

    }

    /**
     * 去填写资料
     */
    private void showInfoDialog() {
        dialog = new CustomDialog(this, R.style.Customdialog, "卡贝贝提示", "您未设置基本资料，请先设置基本资料"
                , new CustomDialog.ButtonListener() {
            @Override
            public void left() {
                dialog.dismiss();
                finish();
            }

            @Override
            public void right() {
                ActivitySkipUtil.skipAnotherActivity(InfoCertifyLoanActivity.this, ModifyInfoActivity.class);
                dialog.dismiss();
                finish();
            }
        }, getString(R.string.cancel), "设置基本资料");
        dialog.show();

        dialog.setOnDismissListener(dialog1 -> finish());
    }

    @Override
    public void loadFaiWorkCard(String errorMsg) {

    }

    @Override
    public void loadSucUpdateImg(ImageUpdate imageUpdate) {

        switch (imgType) {
            case 0:
                Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + imageUpdate.getUrl()).resize(235, 140).centerCrop().into(iv_work_permit);
                workCardImages1 = imageUpdate.getUrl();
                break;
            case 1:
                Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + imageUpdate.getUrl()).resize(235, 140).centerCrop().into(iv_id_card);
                workCardImages2 = imageUpdate.getUrl();
                break;
            case 2:
                Picasso.with(InfoCertifyLoanActivity.this).load(API.PIC_ADDRESS + imageUpdate.getUrl()).resize(235, 140).centerCrop().into(iv_business);
                workCardImages3 = imageUpdate.getUrl();
                break;
        }
    }

    @Override
    public void loadFaiUpdateImg(String errorMsg) {

    }
}
